﻿using System.Text.Json;
using System.Text.Json.Serialization;

namespace Sage.JsonConverters
{
    /// <summary>
    /// 安全的JSON转换器集合
    /// 提供对第三方API返回的不规范JSON数据的安全转换
    /// 所有转换器都绝对不会抛出异常，对于无效数据会返回默认值或null
    /// 支持数字、字符串、布尔值等多种输入类型的自动转换
    /// </summary>
    public static partial class SafeJsonConverters
    {
        // 转换器单例实例，避免重复创建，提高性能

        /// <summary>
        /// 安全的整数转换器实例，失败时返回0
        /// </summary>
        public static readonly SafeIntConverter Int = new();

        /// <summary>
        /// 安全的可空整数转换器实例，失败时返回null
        /// </summary>
        public static readonly SafeNullableIntConverter NullableInt = new();

        /// <summary>
        /// 安全的长整数转换器实例，失败时返回0
        /// </summary>
        public static readonly SafeLongConverter Long = new();

        /// <summary>
        /// 安全的可空长整数转换器实例，失败时返回null
        /// </summary>
        public static readonly SafeNullableLongConverter NullableLong = new();

        /// <summary>
        /// 安全的双精度浮点数转换器实例，失败时返回0.0
        /// </summary>
        public static readonly SafeDoubleConverter Double = new();

        /// <summary>
        /// 安全的可空双精度浮点数转换器实例，失败时返回null
        /// </summary>
        public static readonly SafeNullableDoubleConverter NullableDouble = new();

        /// <summary>
        /// 安全的decimal转换器实例，适合财务计算，失败时返回0m
        /// </summary>
        public static readonly SafeDecimalConverter Decimal = new();

        /// <summary>
        /// 安全的可空decimal转换器实例，适合财务计算，失败时返回null
        /// </summary>
        public static readonly SafeNullableDecimalConverter NullableDecimal = new();

        /// <summary>
        /// 安全的布尔值转换器实例，失败时返回false
        /// </summary>
        public static readonly SafeBoolConverter Bool = new();

        /// <summary>
        /// 安全的可空布尔值转换器实例，失败时返回null
        /// </summary>
        public static readonly SafeNullableBoolConverter NullableBool = new();

        /// <summary>
        /// 安全的DateTime转换器实例，失败时返回DateTime.MinValue
        /// </summary>
        public static readonly SafeDateTimeConverter DateTime = new();

        /// <summary>
        /// 安全的可空DateTime转换器实例，失败时返回null
        /// </summary>
        public static readonly SafeNullableDateTimeConverter NullableDateTime = new();

        /// <summary>
        /// 创建包含所有安全转换器的 JsonSerializerOptions
        /// 适用于处理不可控的第三方API响应
        /// 推荐用于生产环境
        /// </summary>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateSafeOptions()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip
            };

            // 添加所有安全转换器
            options.Converters.Add(Int);
            options.Converters.Add(NullableInt);
            options.Converters.Add(Long);
            options.Converters.Add(NullableLong);
            options.Converters.Add(Double);
            options.Converters.Add(NullableDouble);
            options.Converters.Add(Decimal);
            options.Converters.Add(NullableDecimal);
            options.Converters.Add(Bool);
            options.Converters.Add(NullableBool);
            options.Converters.Add(DateTime);
            options.Converters.Add(NullableDateTime);

            return options;
        }

        /// <summary>
        /// 创建专门用于源生成的安全JsonSerializerOptions
        /// </summary>
        /// <param name="context">源生成的JsonSerializerContext</param>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateSafeOptionsForSourceGeneration(JsonSerializerContext context)
        {
            var options = new JsonSerializerOptions(context.Options)
            {
                PropertyNameCaseInsensitive = true,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip
            };

            // 添加基础类型的安全转换器
            options.Converters.Add(NullableInt);
            options.Converters.Add(NullableLong);
            options.Converters.Add(NullableDouble);
            options.Converters.Add(NullableDecimal);
            options.Converters.Add(NullableBool);
            options.Converters.Add(NullableDateTime);

            return options;
        }

        /// <summary>
        /// 创建只包含可空转换器的JsonSerializerOptions
        /// 推荐用于第三方API集成，null值更容易区分"未设置"状态
        /// </summary>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateNullableOptions()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip
            };

            options.Converters.Add(NullableInt);
            options.Converters.Add(NullableLong);
            options.Converters.Add(NullableDouble);
            options.Converters.Add(NullableDecimal);
            options.Converters.Add(NullableBool);
            options.Converters.Add(NullableDateTime);

            return options;
        }

        /// <summary>
        /// 创建轻量级的JsonSerializerOptions，只包含数值转换器
        /// 适用于性能要求高、只需要数值转换的场景
        /// </summary>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateNumberOnlyOptions()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };

            options.Converters.Add(Int);
            options.Converters.Add(NullableInt);
            options.Converters.Add(Long);
            options.Converters.Add(NullableLong);
            options.Converters.Add(Decimal);
            options.Converters.Add(NullableDecimal);

            return options;
        }

        /// <summary>
        /// 创建只包含基本类型转换器的JsonSerializerOptions
        /// 包含：int, long, decimal, bool (都是可空版本)
        /// 适合处理简单的配置或状态数据
        /// </summary>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateBasicOptions()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };

            options.Converters.Add(NullableInt);
            options.Converters.Add(NullableLong);
            options.Converters.Add(NullableDecimal);
            options.Converters.Add(NullableBool);

            return options;
        }

        /// <summary>
        /// 创建适合财务系统的JsonSerializerOptions
        /// 使用decimal确保精度，包含基本类型转换
        /// </summary>
        /// <returns>配置好的JsonSerializerOptions</returns>
        public static JsonSerializerOptions CreateFinancialOptions()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            options.Converters.Add(NullableInt);
            options.Converters.Add(NullableLong);
            options.Converters.Add(NullableDecimal); // 重点：财务计算使用decimal
            options.Converters.Add(NullableBool);
            options.Converters.Add(NullableDateTime);

            return options;
        }
    }
}